package com.situ.stmall.manager.mapper;

import com.situ.stmall.manager.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    /**
     * 插入
     */
    @Insert({"insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})"})
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int insert(Goods goods);
    /**
     * 根据id删除
     */
    @Delete("delete from goods where id = #{id}")
    int delete(Integer id);
    /**
     * 根据id更新
     */
    @Update({"<script>",
            "update goods",
            "<set>",
            "<if test='name !=null and name.length>0' > name=#{name}, </if>",
            "<if test='dscp !=null and dscp.length>0' > dscp=#{dscp}, </if>",
            "<if test='price !=null' > price=#{price}, </if>",
            "<if test='markPrice !=null' > mark_price=#{markPrice}, </if>",
            "<if test='color !=null and color.length>0' > color=#{color}, </if>",
            "<if test='version !=null and version.length>0' > version=#{version}, </if>",
            "<if test='count !=null' > count=#{count} ,</if>",
            "<if test='content !=null and content.length>0' > content=#{content}, </if>",
            "<if test='recom !=null' > recom=#{recom}, </if>",
            "<if test='categoryId !=null' > category_id=#{categoryId}, </if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    int update(Goods goods);
    /**
     * 根据id查询
     */
    @Select("select * from goods where id = #{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "pics",
            many = @Many(select = "com.situ.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    Goods selectById(Integer id);
    /**
     * 多条件联合查询
     */
    @Select({"<script>",
            "select * from goods",
            "<where>",
            "<if test='name !=null and name.length>0' > name like '%${name}%', </if>",
            "<if test='recom !=null' > recom=#{recom} </if>",
            "<if test='categoryId !=null' > category_id=#{categoryId}, </if>",
            "<if test='status !=null' > and status=#{status} </if>",
            "</where>",
            "</script>"})
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "pics",
                    many = @Many(select = "com.situ.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId")),
            @Result(column = "category_id",property = "category",
                    one = @One(select = "com.situ.stmall.manager.mapper.CategoryMapper.selectById")),
            @Result(column = "category_id",property = "categoryId"),
    })
    List<Goods> select(Goods goods);
}
